package com.sanhe.rfmuscle.view;

import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.menu.app.util.Utils;

import java.util.Objects;

public class PwdEditText extends androidx.appcompat.widget.AppCompatEditText {
    private static final String TAG = "PwdEditText";

    public interface KeyEventClickListener {
        void sendData(int key);

        void enter();

        void backspace();
    }

    private KeyEventClickListener keyEventClickListener;


    public PwdEditText(@NonNull Context context) {
        super(context);
        init();
    }

    public PwdEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PwdEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        if (this.getInputType() != InputType.TYPE_CLASS_NUMBER){
            this.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD | InputType.TYPE_CLASS_NUMBER);
        }
        setImeOptions(EditorInfo.IME_ACTION_DONE);
        setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView arg0, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && actionId == EditorInfo.IME_ACTION_UNSPECIFIED && event.getAction() == KeyEvent.ACTION_DOWN) || actionId == EditorInfo.IME_ACTION_DONE
                        || (event != null
                        && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event
                        .getAction() == KeyEvent.ACTION_DOWN)) {
                    Utils.hideInput(arg0);
                    if (keyEventClickListener != null) {
                        keyEventClickListener.enter();
                    }
                    return true;
                }
                return false;
            }
        });
    }


    @Nullable
    @Override
    public InputConnection onCreateInputConnection(@NonNull EditorInfo outAttrs) {
        return new MyInputConnection(super.onCreateInputConnection(outAttrs), false);
    }

    class MyInputConnection extends InputConnectionWrapper implements InputConnection {

        public MyInputConnection(InputConnection target, boolean mutable) {
            super(target, mutable);
        }

        //退格键回调
        public boolean deleteSurroundingText(int beforeLength, int afterLength) {
            Log.e(TAG, "deleteSurroundingText: ");
            keyEventClickListener.backspace();
            return false;
        }

        public boolean commitText(CharSequence text, int newCursorPosition) {
            Log.e(TAG, "commitText: ");
            return false;
        }

        //DONE按键回调, fix bug: 一次点击多次触发
        public boolean finishComposingText() {
            Log.e(TAG, "finishComposingText: ");
            return super.finishComposingText();
        }

        //数字键回调
        @Override
        public boolean sendKeyEvent(KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getKeyCode() >= KeyEvent.KEYCODE_0
                    && event.getKeyCode() <= KeyEvent.KEYCODE_9) {
                Log.e(TAG, "sendKeyEvent: " + event.getKeyCode());
                if (keyEventClickListener != null) {
                    int key = event.getKeyCode() - 7;
                    keyEventClickListener.sendData(key);
                }
            }
            return false;
        }
    }

    public void setKeyEventClickListener(KeyEventClickListener keyEventClickListener) {
        this.keyEventClickListener = keyEventClickListener;

    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
        this.setSelection(Objects.requireNonNull(this.getText()).length());
    }
}
